package algo;

/**
 * @author Ajie
 * @date 8/6/24
 * @function
 */
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;

public class LineChartExample extends JFrame {

    public LineChartExample(String title, double[] data,String chart_title) {
        super(title);

        // 创建数据集
        XYSeriesCollection dataset = createDataset(data);

        // 创建图表
        JFreeChart lineChart = ChartFactory.createXYLineChart(
                chart_title,
                "Index",
                "Value",
                dataset,
                PlotOrientation.VERTICAL,
                true, true, false);

        // 将图表放入面板中
        ChartPanel chartPanel = new ChartPanel(lineChart);
        chartPanel.setPreferredSize(new Dimension(1200,1200));
        setContentPane(chartPanel);
    }

    private static XYSeriesCollection createDataset(double[] data) {
        XYSeries series = new XYSeries("Distance Origin Fix");

        for (int i = 0; i < data.length; i++) {
            series.add(i, data[i]);
        }

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);

        return dataset;
    }


    public LineChartExample(String title, double[] data, double[] data2,String chart_title) {
        super(title);

        // 创建数据集
        XYSeriesCollection dataset = createDataset(data,data2);

        // 创建图表
        JFreeChart lineChart = ChartFactory.createXYLineChart(
                chart_title,
                "Index",
                "Value",
                dataset,
                PlotOrientation.VERTICAL,
                true, true, false);

        // 获取图表的绘图对象
        XYPlot plot = lineChart.getXYPlot();

        // 设置两条线的颜色
        plot.getRenderer().setSeriesPaint(0, Color.RED);
        plot.getRenderer().setSeriesPaint(1, Color.BLUE);

        // 将图表放入面板中
        ChartPanel chartPanel = new ChartPanel(lineChart);
        chartPanel.setPreferredSize(new Dimension(1200,1200));
        setContentPane(chartPanel);
    }

    private static XYSeriesCollection createDataset(double[] data,double[] data2) {
        XYSeries series = new XYSeries("Data 1");
        XYSeries series2 = new XYSeries("Data 2");

        for (int i = 0; i < data.length; i++) {
            series.add(i, data[i]);
        }
        // 添加第二组数据
        for (int i = 0; i < data2.length; i++) {
            series2.add(i, data2[i]);
        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        dataset.addSeries(series2);
        return dataset;
    }

    //peek A + B
    public LineChartExample(String title, double[] data, double[] data2, int[] peaks1, int[] peaks2,String chart_title) {
        super(title);

        // 创建数据集
        XYSeriesCollection dataset = createDataset(data,data2,peaks1,peaks2);

        // 创建图表
        JFreeChart lineChart = ChartFactory.createXYLineChart(
                chart_title,
                "Index",
                "Value",
                dataset,
                PlotOrientation.VERTICAL,
                true, true, false);

        // 获取图表的绘图对象
        XYPlot plot = lineChart.getXYPlot();

        // 设置两条线的颜色
        plot.getRenderer().setSeriesPaint(0, Color.RED);
        plot.getRenderer().setSeriesPaint(1, Color.BLUE);

        // 设置峰值点的颜色
        plot.getRenderer().setSeriesPaint(2, Color.GREEN);  // Peaks in Data Set 1
        plot.getRenderer().setSeriesPaint(3, Color.ORANGE); // Peaks in Data Set 2

//        // 设置峰值点的颜色和加粗效果
//        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
//        renderer.setSeriesPaint(2, Color.GREEN);  // Peaks in Data Set 1
//        renderer.setSeriesPaint(3, Color.ORANGE); // Peaks in Data Set 2
//
//        // 加粗峰值点的线条
//        renderer.setSeriesStroke(2, new BasicStroke(2.0f));
//        renderer.setSeriesStroke(3, new BasicStroke(2.0f));
//
//        // 使峰值点以点的形式显示并加粗
//        renderer.setSeriesShapesVisible(2, true);
//        renderer.setSeriesShapesVisible(3, true);
//        renderer.setSeriesShape(2, new java.awt.geom.Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0));
//        renderer.setSeriesShape(3, new java.awt.geom.Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0));
//
//        plot.setRenderer(renderer);

        // 将图表放入面板中
        ChartPanel chartPanel = new ChartPanel(lineChart);
        chartPanel.setPreferredSize(new Dimension(1200,1200));
        setContentPane(chartPanel);
    }

   //rssi A + peekA
    public LineChartExample(String title, double[] data, int[] peaks1, String chart_title) {
        super(title);

        // 创建数据集
        XYSeriesCollection dataset = createDataset(data,peaks1);

        // 创建图表
        JFreeChart lineChart = ChartFactory.createXYLineChart(
                chart_title,
                "Index",
                "Value",
                dataset,
                PlotOrientation.VERTICAL,
                true, true, false);

        // 获取图表的绘图对象
        XYPlot plot = lineChart.getXYPlot();

        // 设置两条线的颜色
        plot.getRenderer().setSeriesPaint(0, Color.RED);
        plot.getRenderer().setSeriesPaint(1, Color.BLUE);

        // 设置峰值点的颜色
        plot.getRenderer().setSeriesPaint(2, Color.GREEN);  // Peaks in Data Set 1
        plot.getRenderer().setSeriesPaint(3, Color.ORANGE); // Peaks in Data Set 2

        // 将图表放入面板中
        ChartPanel chartPanel = new ChartPanel(lineChart);
        chartPanel.setPreferredSize(new Dimension(1200,1200));
        setContentPane(chartPanel);
    }

    private static XYSeriesCollection createDataset(double[] data,double[] data2, int[] peaks1, int[] peaks2) {
        XYSeries series = new XYSeries("Data 1");
        XYSeries series2 = new XYSeries("Data 2");
        XYSeries peaksSeries1 = new XYSeries("Peaks in Data Set 1");
        XYSeries peaksSeries2 = new XYSeries("Peaks in Data Set 2");

        for (int i = 0; i < data.length; i++) {
            series.add(i, data[i]);
        }
        // 添加第二组数据
        for (int i = 0; i < data2.length; i++) {
            series2.add(i, data2[i]);
        }
        // 添加第一组数据的峰值点
        for (int peak : peaks1) {
            peaksSeries1.add(peak, data[peak]);
        }

        // 添加第二组数据的峰值点
        for (int peak : peaks2) {
            peaksSeries2.add(peak, data2[peak]);
        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        dataset.addSeries(series2);
        dataset.addSeries(peaksSeries1);
        dataset.addSeries(peaksSeries2);
        return dataset;
    }
    private static XYSeriesCollection createDataset(double[] data, int[] peaks1) {
        XYSeries series = new XYSeries("Data 1");
        XYSeries peaksSeries1 = new XYSeries("Peaks in Data Set 1");

        for (int i = 0; i < data.length; i++) {
            series.add(i, data[i]);
        }
        // 添加第一组数据的峰值点
        for (int peak : peaks1) {
            peaksSeries1.add(peak, data[peak]);
        }
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        dataset.addSeries(peaksSeries1);
        return dataset;
    }

    public static void main(String[] args) {
        // 示例数据
        double[] distance_origin_fix = {10.0, 20.5, 30.3, 40.2, 50.1};

        // 显示图表
        displayChart(distance_origin_fix,"test");
    }

    public static void displayChart(double[] data,String title) {
        SwingUtilities.invokeLater(() -> {
            LineChartExample example = new LineChartExample("Line Chart Example"+title, data ,title);
            example.setSize(1200,1200);
            example.setLocationRelativeTo(null);
            example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            example.setVisible(true);
        });
    }

    public static void displayChart(double[] data,double[] data2,String title) {
        SwingUtilities.invokeLater(() -> {
            LineChartExample example = new LineChartExample("Line Chart Example"+title, data ,data2,title);
            example.setSize(1200,1200);
            example.setLocationRelativeTo(null);
            example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            example.setVisible(true);
        });
    }

    public static void displayChart(double[] data,double[] data2, int[] peaks1, int[] peaks2,String title) {
        SwingUtilities.invokeLater(() -> {
            LineChartExample example = new LineChartExample("Line Chart Example"+title, data ,data2,peaks1,peaks2,title);
            example.setSize(1200,1200);
            example.setLocationRelativeTo(null);
            example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            example.setVisible(true);
        });
    }

    public static void displayChart(double[] data,int[] peaks1,String title) {
        SwingUtilities.invokeLater(() -> {
            LineChartExample example = new LineChartExample("Line Chart Example"+title, data,peaks1,title);
            example.setSize(1200,1200);
            example.setLocationRelativeTo(null);
            example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            example.setVisible(true);
        });
    }
}
